﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using WarningForeverClone.src.util;

namespace WarningForeverClone.src.particles
{
    public class ParticleManager
    {
        static Texture2D hit;
        static Texture2D miss;
        static Texture2D shieldHit;

        Pool<Particle> particles;
        Random rand;
        List<Particle> liveParts;

        public ParticleManager()
        {
            if (hit == null || miss == null)
            {
                if (MainGame.ContentManager != null)
                {
                    hit = MainGame.ContentManager.Load<Texture2D>("Textures/Particles/explosion");
                    miss = MainGame.ContentManager.Load<Texture2D>("Textures/Particles/blast");
                    shieldHit = MainGame.ContentManager.Load<Texture2D>("Textures/Particles/shieldHit");                    
                }
            }

            particles = new Pool<Particle>(500);
            rand = new Random();
            liveParts = new List<Particle>();
        }


        public void CreateHit(Vector2 position)
        {
            const int numParticles = 20;
            for (int x = 0; x < numParticles; x++)
            {
                Particle p = particles.Get();
                p.Reset();

                p.type = 0;
                p.pos = position + new Vector2(rand.Next(10) - 5, rand.Next(10) - 5);
                p.aliveCount = 25;
                p.rotationSpeed = rand.NextDouble() * 0.01f;
                p.rotation = Math.PI * 2 * rand.NextDouble();
                p.velocity = new Vector2((float)(2*rand.NextDouble() - 1), (float)(2*rand.NextDouble() - 1));
                liveParts.Add(p);
            } 

        }
        public void CreateMiss(Vector2 position)
        {
            const int numParticles = 100;
            for (int x = 0; x < numParticles; x++)
            {
                Particle p = particles.Get();
                p.Reset();

                p.type = 1;
                p.pos = position;
                p.aliveCount = 100;
                p.rotationSpeed = rand.NextDouble() * 0.01f;
                p.rotation = Math.PI * 2 * rand.NextDouble();
                p.velocity = new Vector2((float)(2 * rand.NextDouble() - 1), (float)(2 * rand.NextDouble() - 1));
                liveParts.Add(p);
            }

        }
        public void CreateShieldHit(Vector2 position)
        {
            const int numParticles = 20;
            for (int x = 0; x < numParticles; x++)
            {
                Particle p = particles.Get();
                p.Reset();

                p.type = 2;
                p.pos = position + new Vector2(rand.Next(20) - 10, rand.Next(20) - 10);
                p.aliveCount = 25;
                p.rotationSpeed = rand.NextDouble() * 0.01f;
                p.rotation = Math.PI * 2 * rand.NextDouble();
                p.velocity = new Vector2((float)(2 * rand.NextDouble() - 1), (float)(2 * rand.NextDouble() - 1));
                liveParts.Add(p);
            } 
        }

        public void Update()
        {
            for (int x = 0; x < liveParts.Count; x++)
            {
                Particle p = liveParts[x];
                p.Update();

                if (!p.alive)
                {
                    liveParts.RemoveAt(x);
                    particles.Free(p);
                    x--;
                }
            }

        }

        public void Draw(SpriteBatch sb)
        {
            foreach (Particle p in liveParts)
            {
                if (p.alive == false)
                    continue;

                if (p.type == 0)
                {
                    sb.Draw(hit, p.pos, null, Color.White, (float)p.rotation, Vector2.Zero, 0.1f, SpriteEffects.None, 1);
                }
                else if (p.type == 1)
                {
                    sb.Draw(miss, p.pos, null, Color.White, (float)p.rotation, Vector2.Zero, 0.01f, SpriteEffects.None, 1);
                }
                else if (p.type == 2)
                {
                    sb.Draw(shieldHit, p.pos, null, Color.White, (float)p.rotation, Vector2.Zero, 0.05f, SpriteEffects.None, 1);
                }
            }
        }

        public void Clear()
        {
            while (liveParts.Count > 0)
            {
                particles.Free(liveParts[0]);
                liveParts.RemoveAt(0);
            }
        }
        
    }
}
